package com.ql.mangersystem.service.impl;

import com.ql.mangersystem.common.Page;
import com.ql.mangersystem.entity.dto.OrdersPageDTO;
import com.ql.mangersystem.entity.dto.OrdersUpdateDTO;
import com.ql.mangersystem.entity.po.Orders;
import com.ql.mangersystem.mapper.OrdersMapper;
import com.ql.mangersystem.service.OrdersService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Resource
    private OrdersMapper ordersMapper;
    @Override
    public int save(Orders orders) {
        String no = orders.getNo();
        String name = orders.getName();
        String date = orders.getDate();
        BigDecimal money = orders.getMoney();
        String category = orders.getCategory();
        Integer userid = orders.getUserid();
        return ordersMapper.save(no,name,date,money,category,userid);
    }

    @Override
        public int update(Orders orders) {
        String category = orders.getCategory();
        String date = orders.getDate();
        String name = orders.getName();
        BigDecimal money = orders.getMoney();
        Integer id = orders.getId();
        return ordersMapper.update(category,date,name,money,id);
    }

    @Override
    public int delete(Integer id) {
        return ordersMapper.delete(id);
    }

    @Override
    public int deleteBatch(List<Integer> ids,Integer userid) {

        return ordersMapper.deleteBatch(ids,userid);
    }

    @Override
    public Page<Orders> page(OrdersPageDTO ordersPageDTO) {
        String name = ordersPageDTO.getName();
        Integer pageNum = ordersPageDTO.getPageNum();
        Integer pageSize = ordersPageDTO.getPageSize();
        Integer userid = ordersPageDTO.getUserid();
        Integer skipNum = (pageNum - 1) * pageSize;
        Page<Orders> page = new Page<>();
        if (name==null){
            List<Orders> list = ordersMapper.page(skipNum,pageSize,userid);
            Integer count = ordersMapper.count(userid);
            page.setTotal(count);
            page.setList(list);
            return page;
        }
        List<Orders> list =ordersMapper.pageLike(skipNum,pageSize,name,userid);
        Integer count =ordersMapper.countLike(name,userid);
        page.setTotal(count);
        page.setList(list);
        return page;
    }

    @Override
    public Orders getById(Integer id, Integer userid) {
        return ordersMapper.selectById(id,userid);
    }

    @Override
    public List<Orders> list(Integer userid) {
        return ordersMapper.list(userid);
    }
}
